<%--
  Created by IntelliJ IDEA.
  User: dqf
  Date: 2015/3/4
  Time: 10:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<div class="row">
    <!-- NEW WIDGET START -->
    <article class="col-sm-12 col-md-12 col-lg-12">
        <div class="jarviswidget well" id="wid-id-3"
             data-widget-colorbutton="false"
             data-widget-editbutton="false"
             data-widget-togglebutton="false"
             data-widget-deletebutton="false"
             data-widget-fullscreenbutton="false"
             data-widget-custombutton="false"
             data-widget-sortable="false">
            <!-- widget div-->
            <div>
                <div class="widget-body">
                    <a class="btn btn-default" id="linkman-re-common" href="javascript:void(0)">
                        <i class="fa fa-lg fa-mail-reply-all"></i>返回
                    </a>
                    <a class="btn btn-default pull-right pull-right-fix" id="btn-save-common" href="javascript:void(0);"><i class="fa fa-lg fa-download"></i> 保存</a>
                    <hr class="simple">
                    <form id="linkman" class="smart-form" novalidate="novalidate" action="" method="post">
                        <input type="hidden" name="keyId" id="keyId" value="<s:property value="linkman.id" />"/>
                        <header id="title" style="display: block;">
                            联系人信息
                        </header>
                        <fieldset>
                            <div class="row">
                                <label class="label col col-2">
                                    <i class="fa fa-asterisk txt-color-red"></i>
                                    <a  href="javascript:void(0);" key="btn-choose-client"> 客户名称</a>
                                </label>
                                <section class="col col-5">
                                    <label class="input state-disabled">
                                        <input type="text" id="clientName" name="clientName" disabled
                                               value="<s:property value="linkman.client.name"/>"/>
                                        <input type="hidden" id="clientId" name="clientId"
                                               value="<s:property value="linkman.client.id"/>"/>
                                    </label>
                                </section>
                            </div>
                            <div class="row">
                                <label class="label col col-2">
                                    <i class="fa fa-asterisk txt-color-red"></i>
                                    姓名
                                </label>
                                <section class="col col-5">
                                    <label class="input">
                                        <input type="text" name="name" id="linkmanName" placeholder="请输入姓名" value="<s:property value="linkman.name"/>">
                                    </label>
                                </section>
                            </div>
                            <div class="row">
                                <label class="label col col-2">
                                    常用称呼
                                </label>
                                <section class="col col-5">
                                    <label class="input">
                                        <input type="text" name="subName" id="subName" placeholder="请输入常用称呼" value="<s:property value="linkman.subName"/>">
                                    </label>
                                </section>
                            </div>
                            <div class="row">
                                <label class="label col col-2">
                                    <i class="fa fa-asterisk txt-color-red"></i>
                                    职位
                                </label>
                                <section class="col col-5">
                                    <label class="input">
                                        <input type="text" name="duty" id="duty" placeholder="请输入职位" value="<s:property value="linkman.duty"/>">
                                    </label>
                                </section>
                            </div>
                            <div class="row">
                                <label class="label col col-2">
                                    办公电话
                                </label>
                                <section class="col col-5">
                                    <label class="input">
                                        <input type="text" name="tel" id="tel" placeholder="请输入办公电话" value="<s:property value="linkman.tel"/>">
                                    </label>
                                </section>
                            </div>
                            <div class="row">
                                <label class="label col col-2">
                                    <i class="fa fa-asterisk txt-color-red"></i>
                                    手机
                                </label>
                                <section class="col col-5">
                                    <label class="input">
                                        <input type="text" name="mobile" id="mobile" placeholder="请输入手机号码" value="<s:property value="linkman.mobile"/>">
                                        <div id="error"></div>
                                    </label>
                                </section>
                            </div>
                            <div class="row">
                            <label class="label col col-2">备用手机</label>
                            <section class="col col-5">
                                <label class="input">
                                    <input name="mobile_back" id="mobile_back" placeholder="请输入备用手机号"
                                           type="text" value="<s:property value="linkman.mobile_back"/>">
                                </label>
                            </section>
                        </div>
                            <div class="row">
                                <label class="label col col-2">邮箱</label>
                                <section class="col col-5">
                                    <label class="input">
                                        <input name="email" id="email" placeholder="请输入邮箱"
                                               type="text" value="<s:property value="linkman.email"/>">
                                    </label>
                                </section>
                            </div>
                            <div class="row">
                                <label class="label col col-2">
                                    生日</label>
                                <section class="col col-2">
                                    <label class="input">
                                        <i class="icon-append fa fa-calendar"></i>
                                        <input  class="hasDatepicker form-control" name="birthday" id="birthday" placeholder="请选择生日"
                                                type="text" value="<s:date name="linkman.birthday" format="yyyy-MM-dd"/>">
                                    </label>
                                </section>
                            </div>
                            <div class="row">
                                <label class="label col col-2">喜好</label>
                                <section class="col col-5">
                                    <label class="input">
                                        <input name="preferences" id="preferences" placeholder="请输入喜好"
                                               type="text" value="<s:property value="linkman.preferences"/>">
                                    </label>
                                </section>
                            </div>
                            <div class="row">
                            <label class="label col col-2">备注</label>
                            <section class="col col-5">
                                <label class="input">
                                    <input name="remark" id="remark" placeholder="请输入备注"
                                           type="text" value="<s:property value="linkman.remark"/>">
                                </label>
                            </section>
                        </div>
                            <%--<div class="row">--%>
                                <%--<label class="label col col-2">照片</label>--%>
                                <%--<section class="col col-5">--%>
                                    <%--<input  name="uploadify" id="ajax_linkman_file" placeholder="" type="file" >--%>
                                    <%--<input type="hidden" id="fileId" name="fileId" value="<s:property value="fileId" />">--%>
                                    <%--<input type="hidden" id="gridId" name="gridId" value="<s:property value="fileManage.gridId" />">--%>
                                    <%--<div id="img-div" style="display: none;">--%>
                                        <%--<img id="link_img" style="top:0;margin-bottom:0;width:80px;heigth:80px;" src="" />--%>
                                    <%--</div>--%>
                                <%--</section>--%>
                            <%--</div>--%>
                        </fieldset>
                    </form>
                </div>
            </div>
        </div>
    </article>
</div>

<script>
    $(function(){
        var gridId = $("#gridId").val();
        if (gridId != null && gridId != ""){
            $("#img-div").attr("style","display:block;");
            $("#link_img").attr("src","file.action?gridId="+gridId);
        }

        // 手机号码验证
        jQuery.validator.addMethod("isMobile", function(value, element) {
            var length = value.length;
            return this.optional(element) || (length == 11 && /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test(value));
        }, "请正确填写您的手机号码。");
    });



    //选择客户对话框
    $("a[key='btn-choose-client']").unbind("click").bind("click",function(){
//        ajax_action("ajax-dialog!clientDlg.action",{keyIds:$("div.widget-body #clientId").val()},{},function(pdata){
//            oDialog.open({
//                type:"radio",
//                title:"请选择客户",
//                data:pdata.data.data,
//                callback:function(){
//                    var obj =$("#group input:checked");
//                    var name = getArrProp(obj,"key");
//                    var id = getArrProp(obj,"id");
//                    $("div.widget-body #clientName").val(name);
//                    $("div.widget-body #clientId").val(id);
//                }
//            });
//        });
        gDialog.fCreate({
            title:'请选择客户',
            url:"ajax-dialog!client.action",
            width:500
        }).show();
    });

    //校验
    $("#linkman").validate({
        rules : {
            clientId :{
                required : true
            },
            name : {
                required : true,
                remote:{
                    type:"POST",
                    url:"ajax-linkman!isUniqueByName.action",
                    data:{
                        keyId:$("#keyId").val(),
                        clientId:function(){return $("#clientId").val()},
                        validatePro:"name",
                        newValue:function(){return $("#linkmanName").val();}
                    }
                }
            },
            duty:{
                required : true
            },
            mobile:{
                required : true,
                digits:true,
                isMobile : true
            }
        },
        messages : {
            clientId : {
                required : '请选择客户'
            },
            name : {
                required : '请输入联系人名字',
                remote:jQuery.format("该联系人已经被注册")
            },
            duty:{
                required : '请输入职位'
            },
            mobile:{
                required : '请输入手机号',
                digits:'请输入正确的手机号',
                isMobile : "请输入正确的手机号码"
            }
        },
        ignore:"",
        errorPlacement : function(error, element) {
            error.insertAfter(element.parent());
        }
    });

    $('.hasDatepicker').datetimepicker({
        format: 'yyyy-mm-dd',
        weekStart: 1,
        autoclose: true,
        todayBtn: 'linked',
        language: 'zh-CN',
        minView:2
    });
    //保存
    $("#btn-save-common").click(
            function(e) {
                var $valid = $("#linkman").valid();
                if(!$valid) return false;

                $.SmartMessageBox({
                    title : "提示：",
                    content : "确定提交吗？",
                    buttons : '[取消][确认]'
                }, function(ButtonPressed) {
                    if (ButtonPressed === "取消") {
                        e.preventDefault();
                        e.stopPropagation();
                        return;
                    }
                    if (ButtonPressed === "确认") {
                        $("#btn-save-common").attr("disabled", "disabled");
                        var str = "";
                        if ($("#clientId").val() != ""){
                            str = "?clientId="+ $("#clientId").val();
                        }
                        else{
                            str = "?keyId="+ $("#keyId").val();
                        }

                        form_save("linkman","<%=path%>/com/ajax-linkman!save.action"+str);
                         loadURL("ajax-linkman!show.action",$('div#business_data'));
                        //loadURL("ajax!client.action",);
                    }
                });
            }
    );
</script>
<script type="text/javascript">
    //上传文件
    inputLoad({
        objId:"filename",
        entityName:"fileReplyId",
        sourceId:"fileId"
    });

    $(function(){
        var pdata= {
            keyId:$("#keyId").val(),
            flowName:"linkman"
        };
        multiDuty(pdata);
    });
    $("input[name=isDelegate]").unbind().bind("click",function(e){
        $("#agentName").val("");
        if($(this).prop("checked") && $(this).val() == "1"){
            $("#agentDiv").show();
        }else{
            $("#agentDiv").hide();
        }
    });

</script>

<script type="text/javascript">
    function loadCard(){
        area_save($("#province").val(),$("#city").val(),$("#county").val());
        $("a#areaselect_text").show();
    }
    loadCard();
    loadScript("../resource/com/js/data.js", loadSelect);
    function loadSelect(){
        loadScript("../resource/com/js/areaselect.js", loadLocation);
    }
    function loadLocation(){
        var locObj ;
        $("#areaselect_text").click(function(){
            $("#areaselect_span").show();
            locObj = new locationCard({
                ids: ['province', 'city', 'county']
            });
            locObj.init();
            $("#areaselect_text").hide();
        });
        $("#areaselect_span").hide();
        $("#areaselect_check").click(function(){

            if($("#province").val()!=null){
                area_save($("#province").val(),$("#city").val(),$("#county").val());
                $("#areaselect_span").hide();
                $("#areaselect_text").show();
                locObj.destoryCards();
            }else{
                $.smallBox({
                    title : "操作失败",
                    content : "<i class='fa fa-clock-o'></i> <i> 请选择地区，地区不能为空！</i>",
                    color : "#C46A69",
                    iconSmall : "fa fa-times fa-2x fadeInRight animated",
                    timeout : 6000
                });
                locObj.destoryCards();
            }
        })
        $("#areaselect_cancel").click(function(){

            $("#areaselect_span").hide();
            $("#areaselect_text").show();
            locObj.destoryCards();
        })
    }
    function area_save(p,city,county){
        $("a#areaselect_text").text(p+city+county);
        if(p == "") {
            $("a#areaselect_text").text("请选择地址");
        }
    }

    //返回视图
    $("#linkman-re-common").click(function(){
        loadURL("ajax-linkman!show.action",$('div#business_data'));
    });


    //上传图片
    $(function(){
        $("#ajax_linkman_file").uploadify({
            height        	: 30,
            multi : false,
            fileSizeLimit : '5MB',
            fileDataName : 'uploadify',
            progressData : 'percentage',
            'fileTypeDesc' : '格式:jpg',     //描述
            fileTypeExts : '*.jpg',
            buttonText : '选择图片上传',
            swf           : '../resource/com/js/plugin/uploadify/uploadify.swf',
            uploader      : 'ajax-linkman!uploadImg.action?keyId='+$("input#keyId").val(),
            width         : 140,
            onUploadStart:function(file){
                $("#import_message_alert").empty();
                $('<div class="alert alert-info fade in"><button class="close" data-dismiss="alert">×</button><i class="fa-fw fa fa-info"></i><strong>正在努力为您处理！</strong> 导入过程分校验和导入两部分，请耐心等待...</div>').appendTo($("#import_message_alert"));

            },
            onUploadSuccess : function(file, data, response) {
                $("#import_message_alert").empty();
                if(json && json.status == "200"){
                    $('<div class="alert alert-success fade in"><button class="close" data-dismiss="alert">×</button><i class="fa-fw fa fa-check"></i><strong>上传完毕</strong> 相片上传成功！</div>').appendTo($("#import_excel_message_alert"));
                    $(json.rows).each(function(index,map){
                        $('<div class="alert alert-info fade in"><button class="close" data-dismiss="alert">×</button><i class="fa-fw fa fa-info"></i><strong>提醒！</strong> '+map.id+'</div>').appendTo($("#import_excel_message_alert"));
                    });
                    $("#fileId").val(json.gridId);
                    $("#link_img").attr("src",json.fileUrl);
                    $("#img-div").attr("style","display:block");
                }else{
                    $('<div class="alert alert-info fade in"><button class="close" data-dismiss="alert">×</button><i class="fa-fw fa fa-info"></i><strong>上传失败！</strong> </div>').appendTo($("#import_message_alert"));
                }
            }
        });

    });
</script>
